package cn.com.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

public class FunctionUtil {

    /**
     * 函数处理
     *
     * @param branchName 分支机构名称
     * @param tagList    标记列表
     * @return boolean
     */
    public static boolean functionalProcessing(String branchName, Set<String> tagList) {
        switch (branchName.toLowerCase()) {
            case "ls":
            case "ll":
            case "list":
                listTags(tagList);
                return true;
            case "clean":
                clearConsole();
                return true;
            case "exit":
                exitProgram();
                return true;
            case "help":
                displayHelp();
                return true;
            case "version":
                displayVersion();
                return true;
            case "date":
                displayCurrentDate();
                return true;
            default:
                return false;
        }
    }

    private static void listTags(Set<String> tagList) {
        if (tagList.isEmpty()) {
            System.out.println("没有可用的标签。");
        } else {
            System.out.println("可用的标签：");
            tagList.forEach(System.out::println);
        }
    }

    private static void exitProgram() {
        System.out.println("退出程序。");
        System.exit(0);
    }

    private static void displayHelp() {
        System.out.println("可用命令：");
        System.out.println("ls, ll, list    - 列出历史标签");
        System.out.println("clean           - 清除控制台");
        System.out.println("exit            - 退出程序");
        System.out.println("help            - 显示帮助信息");
        System.out.println("version         - 显示程序版本");
        System.out.println("date            - 显示当前日期");
    }

    private static void displayVersion() {
        System.out.println("程序版本：1.0");
    }

    private static void displayCurrentDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("当前日期和时间：" + dateFormat.format(new Date()));
    }

    // Method to clear the console
    public static void clearConsole() {
        try {
            final String os = System.getProperty("os.name");

            if (os.contains("Windows")) {
                // For Windows
                new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor();
            } else {
                // For Unix-like systems
                System.out.print("\033[H\033[2J");
                System.out.flush();
            }
        } catch (Exception e) {
            // Handle exceptions as needed
            e.printStackTrace();
        }
    }
}
